package com.csh.szhm;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.model.LatLng;
import com.csh.szhm.utils.StringUtil;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

import android.app.Application;
import android.widget.ImageView;

public class SzhmApplication extends Application {

	private static SzhmApplication mSzhmApplication;
	private static RequestQueue mRequestQueue;

	@Override
	public void onCreate() {
		initImageLoad();
		initLocation() ;
		mSzhmApplication = this;
		SDKInitializer.initialize(this);
		super.onCreate();
	}

	public static SzhmApplication getInstance() {
		return mSzhmApplication;
	}

	public static RequestQueue getRequestQueue() {
		if (mRequestQueue == null) {
			mRequestQueue = Volley.newRequestQueue(getInstance());
		}
		return mRequestQueue;
	}

	private static ImageLoader mImageLoader;

	private static DisplayImageOptions displayImageOptions;

	private void initImageLoad() {
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				this).denyCacheImageMultipleSizesInMemory()
				.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
				.memoryCacheSize(2 * 1024 * 1024).memoryCacheSize(3) // default
				.diskCacheSize(50 * 1024 * 1024) // 缓冲大小
				.build();
		mImageLoader = ImageLoader.getInstance();
		mImageLoader.init(config);

	}

	public static void display(String url, ImageView img) {
		if (displayImageOptions == null) {
			displayImageOptions = new DisplayImageOptions.Builder()
					.showImageOnLoading(R.drawable.default_loading)
					.showImageOnFail(R.drawable.default_loading)
					.imageScaleType(ImageScaleType.EXACTLY)
					.showImageForEmptyUri(R.drawable.default_loading)
					.displayer(new FadeInBitmapDisplayer(000))
					.cacheInMemory(true).cacheOnDisk(true).build();
		}
		mImageLoader.setChangeImageSize(null);
		mImageLoader.displayImage(url, img, displayImageOptions);

	}

	public static void displayUserDefalue(String url, ImageView img) {
		DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.default_loading)
				.showImageOnFail(R.drawable.default_loading)
				.imageScaleType(ImageScaleType.EXACTLY)
				.showImageForEmptyUri(R.drawable.default_loading)
				.displayer(new FadeInBitmapDisplayer(000)).cacheInMemory(true)
				.cacheOnDisk(true).build();
		mImageLoader.setChangeImageSize(null);
		mImageLoader.displayImage(url, img, displayImageOptions);

	}

	public LocationClient mLocationClient;
	public MyLocationListener mMyLocationListener;

	public void initLocation() {

		if (mLocationClient == null) {
			mLocationClient = new LocationClient(this);
		}
		if (mMyLocationListener == null) {
			mMyLocationListener = new MyLocationListener();
		}
		mLocationClient.registerLocationListener(mMyLocationListener);
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);// 打开gps
		option.setCoorType("bd09ll"); // 设置坐标类型
		option.setScanSpan(15000);
		option.setAddrType("all");
		mLocationClient.setLocOption(option);
		mLocationClient.start();
	}

	public static String mMyLocation;
	public static LatLng mLatLng;

	public class MyLocationListener implements BDLocationListener {

		@Override
		public void onReceiveLocation(BDLocation location) {
			// Receive Location
			StringBuffer sb = new StringBuffer(256);
			sb.append("province : ");
			sb.append(location.getProvince());
			sb.append("city : ");
			sb.append(location.getCity());
			sb.append("dstrict:");
			sb.append(location.getDistrict());
			System.out.println(sb.toString());
			mLatLng = new LatLng(location.getLatitude(),
					location.getLongitude());
			mMyLocation = location.getCity();
			if (!StringUtil.isEmptyString(location.getProvince())) {
				mLocationClient.stop();
//				if (mMyLocation.endsWith("市")) {
//					mMyLocation = mMyLocation.substring(0,
//							mMyLocation.lastIndexOf("市"));
//				}
//				System.out.println("lcoato-->>" + mMyLocation);
				if (mLocationImpl != null) {
					mLocationImpl.getLocation(mMyLocation);
					mLocationImpl = null;
				}
			}
		}

		@Override
		public void onReceivePoi(BDLocation arg0) {
			// TODO Auto-generated method stub

		}
	}

	private locationImpl mLocationImpl;

	public void setLocationImpl(locationImpl l) {
		mLocationImpl = l;
	}

	public interface locationImpl {
		public void getLocation(String city);
	}
}
